package com.vcredit.bird.service.emall;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.base.BirdService;
import com.vcredit.bird.vo.emall.AlipaySummary;
import com.vcredit.creditcat.enumtype.BirdResponeEnum;
import com.vcredit.creditcat.exception.BirdRequestException;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service
public class AlipayProcessService extends BirdService {

	@Value("${alipay.bill.cookieinit}") // cookie登陆初始化
	private String ALIPAY_BILL_COOKIEINIT;

	@Value("${alipay.bill.cookielogin}") // 登陆
	private String ALIPAY_BILL_COOKIELOGIN;

	@Value("${alipay.bill.crawlstatus}") // 查询采集状态
	private String ALIPAY_BILL_CRAWLSTATUS;

	@Value("${alipay.bill.flowermain}") // 查询花呗账单主表信息
	private String ALIPAY_BILL_FLOWERMAIN;

	@Value("${alipay.bill.flowersummary}") // 花呗统计信息
	private String ALIPAY_BILL_FLOWERSUMMARY;

	@Value("${alipay.bill.summary}") // 支付宝变量
	private String ALIPAY_BILL_SUMMARY;
	
	@Value("${alipay.token.init}") // 支付宝，初始化，获取token
	private String ALIPAY_TOKEN_INIT;
	
	@Value("${alipay.qrcode}") // 支付宝采集，获取二维码
	private String ALIPAY_QRCODE;
	
	@Value("${alipay.check.qrcode}") // 支付宝采集，校验二维码
	private String ALIPAY_CHECK_QRCODE;
	
	/**
	 * 默认超时时间,单位为秒
	 */
	private static int DEFAULT_TIMEOUT = 30;
	
	@Log
	public BirdResponse alipayCookieInit() throws Exception {
		BirdResponse response = JacksonUtil.Json2Obj(HttpRequest.doPost(ALIPAY_BILL_COOKIEINIT, new String[] {}),
				new TypeReference<BirdResponse>() {
				});
		if (response.getCode().equals(BirdResponeEnum.HttpRequestSuccess.getCode())) {
			return response;
		} else {
			throw new BirdRequestException("支付宝初始化失败:" + response.getDesc());
		}
	}

	@Log
	public BirdResponse alipayCookieLogin(String userName, String password, String ua, String RdsFormToken,
			String cookies, String token, String bustype) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Username", userName);
		map.put("Password", password);
		map.put("BusType", bustype);
		map.put("Ua", ua);
		map.put("Rds_form_token", RdsFormToken);
		map.put("Cookies", cookies);
		map.put("Token", token);
		try {
			return super.postBirdData(ALIPAY_BILL_COOKIELOGIN, JacksonUtil.Obj2Json(map));
		} catch (Exception e) {
			throw new BirdRequestException("支付宝登陆失败:" + e.getMessage());
		}
	}

	/**
	 * 支付宝登陆，查询采集状态
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@Log
	public BirdResponse alipayCrawlStatus(String token) throws Exception {
		String param = "{\"Token\":\"" + token + "\"}";
		try {
			return super.postBirdData(ALIPAY_BILL_CRAWLSTATUS, param);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("支付宝状态采集失败:" + e.getMessage());
		}
	}

	/**
	 * 支付宝变量
	 * 
	 * @param token
	 * @param name
	 * @param idNo
	 * @return
	 * @throws Exception
	 */
	@Log
	public boolean aplipayIsAuth(String token, String name, String idNo) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Token", token);
		map.put("Name", name);
		map.put("IdentityCard", idNo);
		BirdResponse response = null;
		AlipaySummary summary = null;
		try {
			response = super.postBirdData(ALIPAY_BILL_SUMMARY, JacksonUtil.Obj2Json(map));
			summary = JacksonUtil.Json2Obj(response.getResult(), new TypeReference<AlipaySummary>() {
			});
			if (summary.getIsRealName().equals("是"))
				return true;
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("根据token查询支付宝变量异常:" + e.getMessage());
		}
	}
	
	/**
	 * 支付宝api采集 初始化
	 * @param loginType
	 * @param busType
	 * @return BirdResponse
	 * @throws Exception
	 */
	@Log
	public BirdResponse getORCode(String loginType, String busType) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("IdentityCard", "");
		map.put("Name", "");
		map.put("Username", "");
		map.put("LoginType", loginType);
		map.put("BusType", busType);
		BirdResponse response = super.postBirdData(ALIPAY_TOKEN_INIT, JacksonUtil.Obj2Json(map));
		if (response.getCode().equals(BirdResponeEnum.HttpRequestSuccess.getCode())) {
			return response;
		} else {
			throw new BirdRequestException("支付宝初始化失败:" + response.getDesc());
		}
	}
	
	/**
	 * 支付宝采集，获取二维码
	 * @param loginType
	 * @param busType
	 * @return BirdResponse
	 * @throws Exception
	 */
	@Log
	public BirdResponse getORCode(String token) throws Exception {
		String param = "{\"Token\":\"" + token + "\"}";
		BirdResponse response =  super.postBirdData(ALIPAY_QRCODE, param);
		if (response.getCode().equals(BirdResponeEnum.HttpRequestSuccess.getCode())) {
			return response;
		} else {
			throw new BirdRequestException("支付宝获取二维码:" + response.getDesc());
		}
	}
	/**
	 * 支付宝采集，获取二维码
	 * @param loginType
	 * @param busType
	 * @return BirdResponse
	 * @throws Exception
	 */
	@Log
	public BirdResponse checkORCode(String token, String busType) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Token", token);
		map.put("BusType", busType);
		BirdResponse response =  super.postBirdData(ALIPAY_CHECK_QRCODE, JacksonUtil.Obj2Json(map),new int[]{0,1},DEFAULT_TIMEOUT);
		return response;
	}
}
